/***
This do-file creates a figure showing an example of a structural break identified
in the Affinity data and corrected by our data processing pipeline.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"

*-------------------------------------------------------------------------------
* Prepare data  
*-------------------------------------------------------------------------------
* Load data (before break)
project, uses("${root}/data/dvc/Affinity/Affinity Weekly Before Break Adjustment.dta")
use if county_fips == "36111" using "${root}/data/dvc/Affinity/Affinity Weekly Before Break Adjustment.dta", clear            // fips 36111 is Ulster, NY
destring county_fips, replace

gcollapse (sum) total_spend, by(date county_fips)

* Norm spending to mean of Jan 2020
gegen temp = mean(total_spend) if month(date) == 1 & year(date) == 2020
gegen total_spend_jan2020 = mean(temp)                                                                // just fills in missing values 
assert total_spend_jan2020 != 0 & !mi(total_spend_jan2020)
gen change_spending = (total_spend - total_spend_jan2020) / total_spend_jan2020
replace change_spending = change_spending + 1                                                         // Set "no change" to 1

* Restrict dates to Jan 2020 - Dec 2021
keep if inrange(date, mdy(1, 13, 2020), mdy(12, 31, 2021))

tempfile before_break_adjustment
save `before_break_adjustment'

* Load data (final dataset - already double-normed)
project, uses("${root}/data/web/data/Affinity - County - Daily.csv")
import delimited "${root}/data/web/data/Affinity - County - Daily.csv", clear
rename countyfips county_fips
keep if county_fips == 36111

gen date = mdy(month, day, year)
replace date = date - 6                                                                                // to line dates up with unadjusted data
rename spend_all change_spending_final
replace change_spending_final = change_spending_final + 1                                              // Set "no change" to 1

* Merge to dataset prior to break adjustment
merge 1:1 date county_fips using `before_break_adjustment', keep(match) nogen   
keep date change_spending change_spending_final

*-------------------------------------------------------------------------------
* Plot  
*-------------------------------------------------------------------------------

format %td date
keep if inrange(date, mdy(1, 1, 2021), mdy(12, 31, 2021))                                              // zoom in

line change_spending date, lcolor(oi1%50) lpattern(dash) || line change_spending_final date, lcolor(oi1) ///
yscale(log) ///
ytitle("Change in Spending (%)" "Relative to January 2020") ///
ylab(1 "0%" 1.3 "+30%" 2 "+100%" 4 "+300%" 7 "+600%") ///
xline(`=mdy(8,9,2021)', lcolor(gs8) lpattern(dash)) ///
text(7 `=mdy(8,9,2021)' "Break date: Aug 9, 2021", color(gs8) size(3) placement(east)) ///
xtitle("") ///
xlab(`=mdy(1, 1, 2021)' `""Jan" "2021""'   `=mdy(3, 1, 2021)' "Mar" ///
	 `=mdy(5, 1, 2021)' "May"  ///
	`=mdy(7, 1, 2021)' "Jul"  `=mdy(9, 1, 2021)' "Sep"  `=mdy(11, 1, 2021)' "Nov" ///
	`=mdy(1, 1, 2022)' `""Jan" "2022""' ///
	, labsize(small)) ///
legend(label(1 "Before Adjustments") label(2 "Adjusted") size(small)) 

oi_graph_export "${root}/results/Spending/Structural Break Affinity", type(${fig_type})
